﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BaseSimpleAPI
{
    public class ActionResultHelper
    {
        /// <summary>
        /// 获取响应数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GetResponseData(string data)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var encoding = Encoding.GetEncoding(ResponseHeaderHelper.GetInstance().GetResponseHeader("ResponseEncoding"));
                using (StreamWriter sw = new StreamWriter(ms, encoding))
                {
                    // 响应头
                    foreach (var item in ResponseHeaderHelper.GetInstance().GetResponseHeader())
                    {
                        sw.WriteLine($"item.Key:item.Value");
                    }

                    // 中间分隔
                    sw.WriteLine();

                    // 写入响应数据
                    sw.Write(data ?? string.Empty);
                }

                return ms.ToArray();
            }
        }
    }
}
